package com.itheima.auth.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.model.admin.pojos.Admin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 管理员Mapper
 * 
 * @author itheima
 */
@Mapper
public interface AdminMapper extends BaseMapper<Admin> {

    /**
     * 根据用户名查询管理员
     */
    @Select("SELECT * FROM tb_admin WHERE username = #{username}")
    Admin selectByUsername(@Param("username") String username);

    /**
     * 查询管理员的角色列表
     */
    @Select("SELECT r.role_code FROM tb_admin_role r " +
            "INNER JOIN tb_admin_role_relation rr ON r.id = rr.role_id " +
            "WHERE rr.admin_id = #{adminId} AND r.status = 1")
    List<String> selectRolesByAdminId(@Param("adminId") Integer adminId);

    /**
     * 查询管理员的权限列表
     */
    @Select("SELECT DISTINCT p.permission_code FROM tb_admin_permission p " +
            "INNER JOIN tb_role_permission_relation rpr ON p.id = rpr.permission_id " +
            "INNER JOIN tb_admin_role_relation arr ON rpr.role_id = arr.role_id " +
            "WHERE arr.admin_id = #{adminId} AND p.status = 1")
    List<String> selectPermissionsByAdminId(@Param("adminId") Integer adminId);
}

